What is credit-card-type?
The credit-card-type npm package is used to detect the type of a credit card based on its number. It can identify various card types such as Visa, MasterCard, American Express, and more. This package is useful for validating and formatting credit card information in web applications.
What are credit-card-type's main functionalities?
Detect Card Type
This feature allows you to detect the type of a credit card based on its number. The code sample demonstrates how to use the package to identify a Visa card.
const creditCardType = require('credit-card-type');
const cardInfo = creditCardType('4111111111111111');
console.log(cardInfo);
Get Card Type by Name
This feature allows you to get detailed information about a specific card type by its name. The code sample shows how to retrieve information about Visa cards.
const creditCardType = require('credit-card-type');
const visaCard = creditCardType.getTypeInfo('visa');
console.log(visaCard);
Add Custom Card Type
This feature allows you to add a custom card type to the list of recognized card types. The code sample demonstrates how to add a custom card type and detect it using the package.
const creditCardType = require('credit-card-type');
creditCardType.addCard({
niceType: 'My Custom Card',
type: 'mycustomcard',
patterns: [1234],
gaps: [4, 8, 12],
lengths: [16],
code: { name: 'CVV', size: 3 }
});
const customCardInfo = creditCardType('1234567812345678');
console.log(customCardInfo);
Other packages similar to credit-card-type
card-validator
The card-validator package provides a comprehensive set of validation functions for credit card numbers, expiration dates, and CVV codes. It offers more extensive validation capabilities compared to credit-card-type, which focuses primarily on card type detection.
payment
The payment package offers utilities for formatting and validating payment-related information, including credit card numbers. It provides similar functionality to credit-card-type but also includes additional features for formatting and handling other payment details.
creditcards
The creditcards package is a lightweight library for validating and formatting credit card information. It offers similar card type detection capabilities as credit-card-type but also includes functions for formatting card numbers and expiration dates.
Credit Card Type
Credit Card Type provides a useful utility method for determining a credit card type from both fully qualified and partial numbers. This is not a validation library but rather a smaller component to help you build your own validation or UI library.
This library is designed for type-as-you-go detection (supports partial numbers) and is written in CommonJS so you can use it in Node, io.js, and the browser.
Example
var getCardType = require('credit-card-type');
var card = getCardType('4111');
console.log(card.type);
API
getCardType(number: String)
getCardType
will return an Object with the following data:
Key | Type | Description |
---|
niceType | String | A pretty printed representation of the card brand. - Visa - MasterCard - American Express - DinersClub - Discover - JCB - UnionPay - Maestro |
type | String | A code-friendly presentation of the card brand (useful to class names in CSS). - visa - master-card - american-express - diners-club - discover - jcb |
pattern | RegExp | The regular expression used to determine the card type. |
gaps | Array | The expected indeces of gaps in a string representation of the card number. For example, in a Visa card, 4111 1111 1111 1111 , there are expected spaces in the 4th, 8th, and 12th positions. This is useful in setting your own formatting rules. |
lengths | Array | The expected lengths of the card number as an array of strings (excluding spaces and / characters). |
code | Object | The information regarding the security code for the determined card. Learn more about the code object below. |
code
Card brands provide different nomenclature for their security codes as well as varying lengths.
Brand | Name | Size |
---|
Visa | CVV | 3 |
MasterCard | CVC | 3 |
American Express | CID | 4 |
DinersClub | CVV | 3 |
Discover | CID | 3 |
JCB | CVV | 3 |
UnionPay | CVN | 3 |
Maestro | CVC | 3 |
A full response for a Visa
card will look like this:
{
niceType: 'Visa',
type: 'visa',
pattern: '^4[0-9][\\s\\d]*$',
gaps: [ 4, 8, 12 ],
lengths: [16],
code: { name: 'CVV', size: 3 }
}
Development
We use nvm
for managing our node versions, but you do not have to. Replace any nvm
references with the tool of your choice below.
nvm install
npm install
All testing dependencies will be installed upon npm install
and the test suite executed with npm test
.